﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ahi.Entity;
using Ahi.Config;
using MongoDB.Bson;
using Ahi.Controller;
using Ahi.Entity.Enums;
using Ahi.Entity.Interfaces;
using Ahi.Raporlama;

namespace Ahi.PersonelArama
{
    public partial class PersonelArayanlarForm : Base.BaseForm
    {
        List<PersonelArayanEntity> liste;

        public PersonelArayanEntity CurrentPersonelArayan
        {
            get
            {
                if (bsPersonelArayan.Current is PersonelArayanEntity)
                    return bsPersonelArayan.Current as PersonelArayanEntity;
                return null;
            }
        }

        public PersonelArayanlarForm()
        {
            InitializeComponent();
        }

        public override void Hazirla()
        {
            repositoryItemLookUpAskerlik.SetEnumList<AskerlikDurumlari>();
            repositoryItemLookUpDil.SetEnumList<YabanciDiller>();
        }

        public override void Yukle()
        {
            Listele();
        }

        private void buttonListele_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Listele();
        }

        private void Listele()
        {
            liste = MongoAdaptor.Instance.GetData<PersonelArayanEntity>();
            bsPersonelArayan.DataSource = liste;
        }

        private void buttonEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Duzenle(true);
        }

        private void buttonDuzenle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Duzenle();
        }

        private void Duzenle(bool yeni = false)
        {
            var frm = new PersonelArayanDuzenleForm();
            if (!yeni)
            {
                if (CurrentPersonelArayan == null) return;
                frm.Id = CurrentPersonelArayan.Id;
            }
            frm.ShowDialog();
            Listele();
        }

        private void buttonKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Kaydet();
        }

        private void buttonExceleAktar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ExceleAktar();
        }

        private void ExceleAktar()
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Excel Dosyaları (*.xls)|*.xls";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                gridViewPersonelArayan.ExportToXls(dlg.FileName,true);
            }
        }

        private void buttonYazdir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Yazdir();
        }

        private void Yazdir()
        {
            RaporEntity rapor = null;
            if (RaporSecimForm.ShowRaporSecim(ref rapor, AppConfig.Current.IsArayanlarRaporu) == System.Windows.Forms.DialogResult.OK)
            {
                if (rapor.Id == ObjectId.Empty)
                    gridPersonelArayan.ShowPrintPreview();
                else
                {
                    AppConfig.Current.PersonelArayanlarRaporu = rapor.Id;
                    AppConfig.Current.SaveToDatabase();

                    Raporlama.RaporGoruntuleyiciForm frm = new Raporlama.RaporGoruntuleyiciForm();
                    frm.Id = AppConfig.Current.PersonelArayanlarRaporu;
                    frm.DataSource = bsPersonelArayan.DataSource;
                    frm.Show();
                }
            }
        }

        private void buttonKapat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Close();
        }

        private void gridViewPersonelArayan_DoubleClick(object sender, EventArgs e)
        {
            Duzenle();
        }
    }
}
